<?php
namespace Api\Controller;
use Think\Controller; 
class SeedsController extends Controller {
	
	//种子列表
	public function entry(){
		$pageno = I('get.pageno',1);
		$site_id = I('get.site_id','全部');
		$pagesize = C('PAGESIZE');
		if($site_id != '全部'){
			$where['site_id'] = $site_id;	
		}
		$Sites = M('Sites');
		$site = $Sites -> where(array('id' => $site_id)) -> select();
		$Seeds =  M('Seeds'); 
		$data = $Seeds->limit($pagesize)->where($where)->page($pageno)-> select();
		if(count($data) > 0){
			$ret['status'] = true;	
			$ret['count'] = count($data);
			$ret['data'] = $data;
			$ret['ext']['pageSize'] = $pagesize;
			$totals = $Seeds->field('count(*) as total')->where($where)->select();
			$ret['ext']['totalCounts'] = $totals[0]['total'];
		}else if(count($data) == 0){
			$ret['status'] = false;	
			$ret['count'] = 2;
			$ret['data'] = 'Empty!';
		}else {
			$ret['status'] = false;	
			$ret['count'] = 3;
			$ret['data'] = 'Query Failed!';
		}
		$ret['ext']['site'] = $site[0];
		$this->ajaxReturn($ret);
	}

	//根据id查找种子
	public function qbyid(){
		$id = I('get.id');	
		if(!$id){
			$ret['status'] = false;	
			$ret['count'] = 2;
			$ret['data'] = 'Invalid Parameter!';
			$this->ajaxReturn($ret);
			return;
		}
		$Seeds = M('Seeds');
		$where = 'id='.$id;
		$req = $Seeds -> where($where) -> select();
		if(count($req) != 0){
			$ret['status'] =  true;
			$ret['count'] = count($req);
			$ret['data'] = $req;
		}else if(count($req) == 0){
			$ret['status'] = false;	
			$ret['count'] = 2;
			$ret['data'] = 'Enpty!'; 
		}else {
			$ret['status'] = false;	
			$ret['count'] = 3;
			$ret['data'] = 'Query Failed!'; 
		}	
		$this->ajaxReturn($ret);
	}
	
	//对站点添加种子
	public function add(){
		$site_id = I('post.site_id');
		if(!$site_id){
			$ret['status'] = false;	
			$ret['count'] = 2;
			$ret['data'] = 'Invalid Parameter!';
			$this->ajaxReturn($ret);
			return;
		}
		//检测站点是否存在
		$Sites = M('Sites');
		$site = $Sites -> where('id='.$site_id) -> select();
		if(!$site){
			$ret['status'] = false;	
			$ret['count'] = 5;
			$ret['data'] = 'The Site Not Exist!';
			$this->ajaxReturn($ret);
			return;	
		}
		//插入站点
		$Seeds = D("Seeds"); // 实例化User对象
		if (!$Seeds -> create()){ // 创建数据对象
			// 如果创建失败 表示验证没有通过 输出错误提示信息
			$ret['status'] = false;	
			$ret['count'] = 3;
			$ret['data'] = 'Insert Failed!'; 
		}else{
			// 验证通过 写入新增数据
			$readd = $Seeds -> add();
			if($readd){
				$ret['status'] = true;
				$ret['count'] = 0;
				$ret['data'] = "Insert Success!";
			}else {
				$ret['status'] = false;	
				$ret['count'] = 3;
				$ret['data'] = 'Insert Failed!'; 
			}
		}
		$this->ajaxReturn($ret);
	}

	//编辑种子
	public function edit(){
		$id = I('get.id');
		if(!$id){
			$ret['status'] = false;	
			$ret['count'] = 0;
			$ret['data'] = 'Invalid Parameter!';
			$this->ajaxReturn($ret);
			return;
		}
		//插入站点
		$Seeds = D('Seeds'); // 实例化User对象
		if (!$Seeds -> create()){ // 创建数据对象
			// 如果创建失败 表示验证没有通过 输出错误提示信息
			$ret['status'] = false;	
			$ret['count'] = 3;
			$ret['data'] = 'Create Failed!'; 
		}else{
			// 验证通过 写入新增数据
			$redit = $Seeds -> where(array('id' => $id)) -> save();
			if($redit){
				$ret['status'] = true;
				$ret['count'] = 0;
				$ret['data'] = "Update Success!";
			}else {
				$ret['status'] = false;	
				$ret['count'] = 3;
				$ret['data'] = 'Update Failed!'; 
			}
		}
		$this->ajaxReturn($ret);
	}
}

?>

















